package com.jfcom.directorymonitor.service.impl;

import com.jfcom.directorymonitor.domain.CiFileRecord;
import com.jfcom.directorymonitor.repository.DiFileRecordRepo;
import com.jfcom.directorymonitor.service.FileTreeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class FileTreeRecordServiceImpl implements FileTreeRecordService {
    @Autowired
    DiFileRecordRepo diFileRecordRepo;

    @Override
    public Optional<CiFileRecord> getCiFileRecordByFullPath(String fullPath) {
        Optional<CiFileRecord> byFullPath = diFileRecordRepo.findByFullPath(fullPath);
        if (byFullPath.isEmpty()) {
            log.info("No record found for fullPath: {}", fullPath);
            return Optional.empty();
        }

        return byFullPath;
    }

    @Override
    public CiFileRecord save(CiFileRecord record) {
        return diFileRecordRepo.save(record);
    }

    @Override
    public void deleteById(Long id) {
         diFileRecordRepo.deleteById(id);
    }

    @Override
    public List<CiFileRecord> findAll() {
        return diFileRecordRepo.findAll();
    }

}
